﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WeiXin.SDK.Entities;

namespace WeiXin.SDK
{
    public class MessageAPI
    {
        public static bool SendTemplateMessage(string access_token, string openId, string templateId, object data, string url)
        {
            string postUrl = string.Format("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={0}", access_token);
            var msgData = new TemplateData()
            {
                touser = openId,
                template_id = templateId,
                url = url,
                data = data
            };
            string postData = Newtonsoft.Json.JsonConvert.SerializeObject(msgData);
            var result = HttpClientHelper.PostResponse<MessageResult>(postUrl, postData);
            if (result.errcode != ReturnCode.请求成功)
            {
                throw new Exception("错误" + result.ToString());
            }
            return result.errcode == 0;

        }
    }
}
